package com.ruoyi.web.controller.system;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CityData;
import com.ruoyi.teaching.domain.TeachingSchool;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.teaching.domain.TeachingSchool;
import com.ruoyi.teaching.domain.TeachingStudent;
import com.ruoyi.teaching.service.ITeachingSchoolService;
import com.ruoyi.teaching.service.ITeachingStudentService;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@Controller
public class SysRegisterController extends BaseController {
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ITeachingStudentService studentService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ITeachingSchoolService teachingSchoolService;


    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public AjaxResult ajaxRegister(@RequestParam("birthday") String birthday,@RequestParam("schoolId") Long schoolId, SysUser user) {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return error("当前系统没有开启注册功能！");
        }

        String msg = registerService.register(user);
        roleService.insertDefaultRole(user.getUserId());
        if (StringUtils.isEmpty(msg)) {
            TeachingStudent student = new TeachingStudent();
            if (StringUtils.isEmpty(birthday)) {
                student.setBirthday(DateUtils.parseDate(birthday));
            } else {
                student.setBirthday(DateUtils.parseDate(birthday));
            }
            student.setPhonenumber(user.getPhonenumber());
            student.setSchoolId(schoolId);
            student.setSex(user.getSex());
            student.setStudentName(user.getUserName());
            student.setUserId(user.getUserId());
            studentService.insertTeachingStudent(student);
        }
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 三级联动 省份区
     * @return
     */
    @GetMapping("/register/cityData")
    @ResponseBody
    public List<CityData> cityData() {
        List<CityData> listP = new ArrayList<>();
        //获取省份
        List<TeachingSchool> list = teachingSchoolService.selectTeachingSchoolList(new TeachingSchool());
        for (TeachingSchool t : list) {
            CityData province = new CityData();
            province.setCode(t.getProvinceCode());
            province.setN(t.getProvinceName());
            if (listP.stream().filter(a -> a.getCode().equals(province.getCode())).count() == 0) {
                listP.add(province);
            }
            List<CityData> listC = new ArrayList<>();
            TeachingSchool paramC = new TeachingSchool();
            paramC.setProvinceCode(province.getCode());
            List<TeachingSchool> cities = teachingSchoolService.selectTeachingSchoolList(paramC);
            for (TeachingSchool c : cities) {
                CityData city = new CityData();
                city.setCode(c.getCityCode());
                city.setN(c.getCityName());
                if (listC.stream().filter(a -> a.getCode().equals(city.getCode())).count() == 0) {
                    listC.add(city);
                }
                List<CityData> listA = new ArrayList<>();
                TeachingSchool paramA = new TeachingSchool();
                paramA.setCityCode(city.getCode());
                List<TeachingSchool> areas = teachingSchoolService.selectTeachingSchoolList(paramA);
                for (TeachingSchool a : areas) {
                    CityData area = new CityData();
                    area.setCode(a.getAreaCode());
                    area.setN(a.getAreaName());
                    if (listA.stream().filter(f -> f.getCode().equals(area.getCode())).count() == 0) {
                        listA.add(area);
                    }
                    city.setS(listA);
                }
                province.setS(listC);
            }

        }
        return listP;
    }

    /**
     * 四级联动 校区选择
     */
    @GetMapping("/register/select")
    @ResponseBody
    public List<TeachingSchool> select(Long areaCode) {
        List<TeachingSchool> list1 = teachingSchoolService.selectTeachingSchoolByAreaCode(areaCode);
        return list1;
    }
}
